import axios from "axios";

// 获取角色
export function getRole() {
  return axios
    .get("/config/role.json")
    .then((res) => {
      return res.data;
    })
    .catch((error) => {
      console.error("Error fetching role:", error);
      return {};
    });
}

// 权限匹配
export function getPermission(data) {
  return axios
    .get("/config/permission.json")
    .then((res) => {
      const result = {};
      const menuData = res.data;
      for (const key in menuData) {
        if (key === "menuTemplate") continue;
        const displayMenus = [];
        const showMenuIndices = menuData[key].showMenus;
        for (const index of showMenuIndices) {
          displayMenus.push(menuData.menuTemplate[index]);
        }
        result[key] = displayMenus;
      }
      return result[data];
    })
    .catch((error) => {
      console.error("Error fetching role:", error);
      return {};
    });
}
